package com.oa.android.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.baidu.location.BDLocation;
import com.baidu.location.BDLocationListener;
import com.baidu.location.LocationClient;
import com.baidu.location.LocationClientOption;
import com.baidu.mapapi.map.BaiduMap;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdate;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.model.LatLng;
import com.oa.android.R;
import com.oa.android.utils.UConstants;

import java.text.SimpleDateFormat;
import java.util.Date;

import butterknife.ButterKnife;
import butterknife.InjectView;


public class QianDaoActivity extends BaseActivity implements View.OnClickListener{

    @InjectView(R.id.close)
    ImageView close;
    @InjectView(R.id.title_text)
    TextView titleText;
    @InjectView(R.id.ritgh_icon)
    ImageView ritghIcon;
    @InjectView(R.id.ritgh_text)
    TextView ritghText;
    @InjectView(R.id.time)
    TextView time;
    @InjectView(R.id.qdTime)
    TextView qdTime;
    @InjectView(R.id.comName)
    TextView comName;
    @InjectView(R.id.address)
    TextView address;
    @InjectView(R.id.mapView)
    MapView mapView;
    @InjectView(R.id.qdLayout)
    LinearLayout qdLayout;
    private BaiduMap mBaiduMap;

    private LocationClient mLocationClient = null;
    private BDLocationListener myListener;
    private String addStr;
    private String lng;
    private String locationdesribe;
    SimpleDateFormat simple;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_qd);
        ButterKnife.inject(this);
        mLocationClient = new LocationClient(this); // 声明LocationClient类
        myListener = new MyLocationListener();
        mLocationClient.registerLocationListener(myListener); // 注册监听函数
        mLocationClient.setLocOption(InitLocOption());

        mLocationClient.start();// 开始定位
        titleText.setText("签到");
         simple = new SimpleDateFormat("HH:mm");
        qdTime.setText(""+simple.format(new Date()));
        getTimes();
        time.setText(new SimpleDateFormat("yyyy年MM月dd日").format(new Date()));
        qdLayout.setOnClickListener(this);
        comName.setText(""+ UConstants.ComName);
        close.setOnClickListener(this);
        mBaiduMap = mapView.getMap();
        mBaiduMap.setMapType(BaiduMap.MAP_TYPE_NORMAL);//普通地图
        mapView.showScaleControl(false);

        // 不显示地图缩放控件（按钮控制栏）
        mapView.showZoomControls(false);
    }

    private void getTimes(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(10*1000);
                    handler.sendEmptyMessage(1);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private Handler handler = new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            qdTime.setText(""+simple.format(new Date()));
            getTimes();
        }
    };

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.close:
                finish();
                break;
            case R.id.qdLayout:
                if (TextUtils.isEmpty(lng)){
                    showToast("定位获取失败,请检查软件定位权限");
                    return;

                }
                Intent intent = new Intent(this,SendPictureActivity.class);
                intent.putExtra("str",""+locationdesribe+","+addStr);
                intent.putExtra("lng",lng);
                startActivity(intent);
                break;
        }
    }

    private LocationClientOption InitLocOption(){
        LocationClientOption option = new LocationClientOption();
        option.setLocationMode(LocationClientOption.LocationMode.Hight_Accuracy);//默认高精度定位模式
        option.setCoorType("bd09ll");//默认gcj02，设置返回的定位结果坐标系
        option.setScanSpan(0);//默认0，即仅定位一次，设置发起定位请求的间隔需要大于等于1000ms
        option.setIsNeedAddress(true);//设置是否需要地址信息，默认不需要
        option.setOpenGps(true);//默认false,设置是否使用gps
        option.setLocationNotify(true);//默认false，设置是否当GPS有效时按照1S/1次频率输出GPS结果
        option.setIsNeedLocationDescribe(true);//默认false，设置是否需要位置语义化结果
        option.setIsNeedLocationPoiList(true);//默认false，设置是否需要POI结果
        option.setIgnoreKillProcess(false);//默认true不杀死进程，设置是否在stop的时候杀死进程
        option.SetIgnoreCacheException(false);//默认false，设置是否收集CRASH信息，默认收集
        option.setEnableSimulateGps(false);//默认false，设置是否需要过滤GPS仿真结果，默认需要
        return option;
    }


    private class MyLocationListener implements BDLocationListener {
        @Override
        public void onReceiveLocation(BDLocation location) {
            if(location == null){
                Toast.makeText(QianDaoActivity.this,"location is null",Toast.LENGTH_SHORT).show();
                return;
            }
            BitmapDescriptor bitmapDescriptor = BitmapDescriptorFactory.fromResource(R.mipmap.map_icon);
            LatLng latlng = new LatLng(location.getLatitude(), location.getLongitude());//创建一个图层选项
            OverlayOptions options = new MarkerOptions().position(latlng).icon(bitmapDescriptor);
            mBaiduMap.addOverlay(options);
            MapStatus mMapStatus = new MapStatus.Builder().target(latlng).zoom(19.0f).build();
            //定义MapStatusUpdate对象，以便描述地图状态将要发生的变化
            MapStatusUpdate mMapStatusUpdate = MapStatusUpdateFactory.newMapStatus(mMapStatus);
            mBaiduMap.setMapStatus(mMapStatusUpdate);//改变地图状态
//            address.setText(""+location.getAddrStr());
            addStr = location.getAddrStr();
            locationdesribe = location.getLocationDescribe();
            if(!TextUtils.isEmpty(locationdesribe)){

            locationdesribe = locationdesribe.replace("在","");
            locationdesribe = locationdesribe.replace("附近","");
            }
            address.setText(""+locationdesribe);
            lng = location.getLongitude()+","+location.getLatitude();
            if (null!=location){
                mLocationClient.stop();
            }
        }
    }

    @Override
    protected void onDestroy() {
        if(mLocationClient != null && mLocationClient.isStarted()){
            if(myListener != null){
                mLocationClient.unRegisterLocationListener(myListener);
            }
            mLocationClient.stop();
            mLocationClient = null;
        }
        super.onDestroy();
    }

}
